package com.mfangsoft.zhuangjialong.app.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.customer.service.ICustomerService;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;

@Controller("appCustomerController")
@RequestMapping("app")
public class CustomerController {

	@Autowired
	ICustomerService customerService;
	
	@RequestMapping(value="/selectSellerCustomer",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<Map<String,Object>>> selectSellerCustomer(@RequestBody Page<Map<String,Object>> page){
		ResponseMessage<Page<Map<String,Object>>> message = new ResponseMessage<Page<Map<String,Object>>>();
		try {
			message.setResult(customerService.selectSellerCustomer(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/selectSellerCustomerDetail",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> selectSellerCustomerDetail(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			message.setResult(customerService.selectSellerCustomerDetail(map));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/selectCustomerDataForPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<Map<String,Object>>> selectCustomerDataPage(@RequestBody Page<Map<String,Object>> page){
		ResponseMessage<Page<Map<String,Object>>> message = new ResponseMessage<Page<Map<String,Object>>>();
		try {
			message.setResult(customerService.selectCustomerDataPage(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}

	@RequestMapping(value="/selectCustomerDataDistribution",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public Map<String,Object> selectCustomerDataDistributionForPage(HttpServletRequest request){
		
		Map<String,Object> map = new HashMap<String,Object>();
		String size = request.getParameter("size");
		if(size ==null || "".equals(size)){
			map.put("returncode", 1);
			map.put("returnmsg", "size 为空");
			return map;
		}
		try {
			List<Map<String,Object>> list =  customerService.selectCustomer1DataPage(Long.parseLong(size));
			map.put("returncode", 0);
			map.put("returnmsg", "ok");
			if(list!=null && !list.isEmpty()){
				map.put("fNum", list.size());
			}else{
				map.put("fNum", 0);
			}
			map.put("fCusts", list);
			
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("returncode", 1);
			map.put("returnmsg", "网络异常");
			return map;
		}
	}
	
	
	
}
